<?php
/*
  [EWEISHOP] Copyright(C) 2017 Elinkint Inc.
  This is NOT a freeware, use is subject to license terms
   
  OfficialAccountTemplateMessage.php 2018-05-24 17:17 likexin
   
*/

namespace common\library\wechat\officialaccount;


use common\component\wechat\Wechat;
use common\library\wechat\WechatApi;
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;

class OfficialAccountTemplateMessage extends WechatApi
{

    public static $apiType = 'officialAccount';
    /**
     * 修改账号所属行业
     * @param string $industryId1 所属行业1
     * @param string $industryId2 所属行业2
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/template_message
     */
    public static function setIndustry(string $industryId1, string $industryId2)
    {
        $result = self::getInstance()->template_message->setIndustry($industryId1, $industryId2);
        return Wechat::apiError($result);
    }

    /**
     * 获取支持的行业列表
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/template_message
     */
    public static function getIndustry()
    {
        $result = self::getInstance()->template_message->getIndustry();
        return Wechat::apiError($result);
    }

    /**
     * 添加模板
     * @param string $shortId 在公众号后台获取shortId并添加到账户
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/template_message
     */
    public static function add(string $shortId)
    {
        $result = self::getInstance()->template_message->addTemplate($shortId);
        return Wechat::apiError($result);
    }

    /**
     * 获取已添加所有模板列表
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/template_message
     */
    public static function getTemplates()
    {
        $result = self::getInstance()->template_message->getPrivateTemplates();
        return Wechat::apiError($result);
    }

    /**
     * 删除指定已添加模板
     * @param string $templateId 模板ID
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/template_message
     */
    public static function delete(string $templateId)
    {
        $result = self::getInstance()->template_message->deletePrivateTemplate($templateId);
        return Wechat::apiError($result);
    }

    /**
     * 发送模板消息
     * @param array $message 消息对象信息
     * @param array $message[touser] 目标用户OPENID
     * @param array $message[template_id] 已申请的模板ID
     * @param array $message[url] URL地址
     * @param array $message[data] 模板数据
     * @param array $message[data][] 数据格式 'key'=>'value' | 'key'=>['value', 'color'] | 'key'=>['value'=>'value', 'color'=>'color']
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/official-account/template_message
     */
    public static function send(array $message)
    {
        try {
            $result = self::getInstance()->template_message->send($message);
        } catch (InvalidArgumentException $exception){
            return error($exception->getMessage());
        }
        return Wechat::apiError($result);
    }

}